<template>
	<uni-popup ref="popup" type="bottom" @change="change">
		<view class="popup-content d-flex flex-column">
			<view class="d-flex justify-content-end">
				<image src="/static/images/common/clousex-big.png" class="close_btn" @tap="close"></image>
			</view>
			<view class="d-flex flex-fill flex-column justify-content-between">
				<view class="d-flex flex-column">
					<view class="font-size-extra-lg font-weight-bold text-color-base mb-30">欢迎登陆雨瑞商贸~</view>
					<view class="font-size-base text-color-assist">登录后消费可获取积分，享受更好的服务体验</view>
				</view>
				<view class="d-flex flex-column">
					<button type="primary" class="w-100 font-size-lg mb-30" open-type="getUserInfo" @getuserinfo="getUserInfo">微信一键登陆</button>
					<view class="text-center mb-30 font-size-sm text-color-assist">
						点击登陆雨瑞商贸，即表示已阅读并同意<font class="text-color-primary">《雨瑞商贸隐私政策》</font>
					</view>
					<view class="text-center font-primary font-size-sm text-color-primary">《小程序服务指南》</view>
				</view>
			</view>
		</view>
	</uni-popup>
</template>

<script>
	import uniPopup from '@/components/uni-popup/uni-popup.vue'
	import { mapMutations,mapState } from 'vuex'
	
	export default {
		name: 'LoginPopup',
		components: {
			uniPopup
		},
		props: {
			
		},
		onLoad() {
			// this.auth();
		},
		computed:{
			...mapState(['isAuth','isLogin'])
		},
		methods: {
		...mapMutations(['SET_USERINFO', 'SET_ISLOGIN','SET_AUTH','SET_TOKEN','SET_ADDRESS','SET_ORDER','SET_CARSOUSEL']),
		
			open() {
				this.$refs['popup'].open()
			},
			close() {
				this.$refs['popup'].close()
			},
			change({show}) {
				this.$emit('change', show)
			},
			 auth(){
				let that=this;
				return new Promise((reslove,reject)=>{
					if(!that.isAuth){
						uni.getSetting({
													      success: data => {
															  //判断是否授权
													        if (data.authSetting['scope.userInfo']) {
																that.SET_AUTH(true)
																reslove({});
													        } else {//null或者是false,表示没有授权，请求授权
															uni.authorize({
																scope:'scope.userInfo',
																success:res=>{
																	that.SET_AUTH(true);
																	reslove({});
																},
																fail:err=>{
																	that.SET_AUTH(false)
																	console.log('授权失败')
															
																	 reject();
																}
																,complete: () => {
																	
																}
															})
													        
													        }
													      },
														  fail: function(error) {
															  that.SET_AUTH(false)
															console.log(error);
														  }
													    })
					}else{
						reslove({});
					}
					
				})
			},
			getUserInfo(e) {
				let that=this;
				
				 this.auth().then(res=>{
					uni.login({
						success(login_res) {
							if(login_res.errMsg==='login:ok'){
								
								uni.getUserInfo({
									success:userinfo=>{
										console.log('你好')
										if(userinfo){
											//执行登录逻辑
											let req={
												code: login_res.code, //临时登录凭证
																			rowData: userinfo.rawData, //用户非
																			signature: userinfo.signature, //签名
																			encryptData: userinfo.encryptedData, 
																			iv: userinfo.iv //解密算法的向量
											}
											that.$u.api.login(req).then(check=>{
												that.SET_ISLOGIN(true)
												let user={...userinfo.userInfo,... check};
																							
												that.SET_USERINFO(user);
												uni.setStorageSync('userinfo',user)
												that.getAddress(check.uid);
												 that.getCarousels();
												 that.getOrders();
											}).catch(err=>{
												uni.showModal({
														title: '提示',
														content: `${process.env.NODE_ENV}`,
												 		showCancel: false
												 	})
												that.SET_ISLOGIN(false)
												console.log(err)
											})
	
										}else{
											uni.showModal({
													title: '提示',
													content: '获取用户信息失败',
											 		showCancel: false
											 	})
												that.SET_ISLOGIN(false)
												return;
										}
									
									}
								})
							}else{
								uni.showModal({
										title: '提示',
										content: '登录失败,请稍后重试',
								 		showCancel: false
								 	})
									that.SET_ISLOGIN(false)
									return;
							}
							
							
							
						},
					
					})
					 this.close()
				 }).catch(err=>{
					 uni.showModal({
					 		title: '提示',
					 		content: '您已取消了授权，请重新授权登录',
					  		showCancel: false
					  	})
						return;
				 })
				// if(e.target.errMsg !== 'getUserInfo:ok') {
				// 	uni.showModal({
				// 		title: '提示',
				// 		content: '您已取消了授权，请重新授权登录',
				// 		showCancel: false
				// 	})
				// 	return
				// }else{
					
				// }
				//
				
			},
			getAddress(uid){
				 let that=this;
			 this.$u.api.address.get(uid).then(res=>{
				 let resArray=[];
				 if(!that.$u.test.isEmpty(res)){
					 resArray=res;
				 }
				 that.SET_ADDRESS(resArray)
			 })
			},
		async getCarousels(){
			let that=this;
			 that.$u.api.carousels().then(res=>{
				that.SET_CARSOUSEL(res.map(data=>data.imgUrl))
			})
		},
		async getOrders() {
			let that=this;
			that.$u.api.orders.get().then(res=>{
				that.SET_ORDER(res);
			})
			
		},
		}
	}
</script>

<style lang="scss" scoped>
	.popup-content {
		background-color: #FFFFFF;
		border-radius: 24rpx 24rpx 0 0;
		height: 50vh;
		padding: 50rpx 40rpx;
	}
	
	.close_btn {
		width: 40rpx;
		height: 40rpx;
	}
</style>
